﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;

namespace Inovout.Runtime
{
    public class ApplicationProperties
    {
        private static readonly Dictionary<string, string> properties;

        static ApplicationProperties()
        {
            properties = new Dictionary<string, string>();
            Uri binUri = new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase));
            string applicationConfigFile = Path.Combine(binUri.LocalPath, "Configs/Application.xml");
            if (!File.Exists(applicationConfigFile))
            {
                applicationConfigFile = Path.Combine(binUri.LocalPath, "../Configs/Application.xml");
                if (!File.Exists(applicationConfigFile))
                {
                    return;
                }
            }
            XmlDocument applicationPropertiesXml = new XmlDocument();
            applicationPropertiesXml.Load(applicationConfigFile);
            var propertyNodes = applicationPropertiesXml.SelectNodes("//properties/property");
            foreach (XmlNode node in propertyNodes)
            {
                properties.Add(node.Attributes["name"].Value, node.InnerText);
            }
        }

        public static Dictionary<string, string> GetProperties()
        {
            return properties;
        }
    }
}
